package com.ruoyi.t.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.t.domain.*;
import com.ruoyi.t.mapper.*;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.t.service.ITOrderService;

/**
 * 订单Service业务层处理
 *
 * @author YY
 * @date 2025-02-06
 */
@Service
public class TOrderServiceImpl implements ITOrderService
{
    @Autowired
    private TOrderMapper tOrderMapper;

    @Autowired
    private TOrderStyleMapper tOrderStyleMapper;

    @Autowired
    private TOrderClientPaymentMapper tOrderClientPaymentMapper;

    @Autowired
    private TOrderUserAddressMapper tOrderUserAddressMapper;

    @Autowired
    private TOrderUserPaymentMapper tOrderUserPaymentMapper;
    /**
     * 查询订单
     *
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public TOrder selectTOrderById(Long id)
    {
        TOrder tOrder1 = tOrderMapper.selectTOrderById(id);
        TOrderStyle t = new TOrderStyle();
        t.setOrderId(tOrder1.getId());
        List<TOrderStyle> tOrderStyleList = tOrderStyleMapper.selectTOrderStyleList(t);

        tOrder1.setMap(tOrderStyleList);
        return tOrder1;
    }

    /**
     * 查询订单列表
     *
     * @param tOrder 订单
     * @return 订单集合
     */
    public List<TOrder> selectTOrderListUser(TOrder tOrder){
        List<TOrder> list = tOrderMapper.selectTOrderList(tOrder);
        for(TOrder tOrder1 : list){
            TOrderStyle t = new TOrderStyle();
            t.setOrderId(tOrder1.getId());
            if(!"1".equals(tOrder.getIsMaster())){
                t.setUserId(SecurityUtils.getUserId());
            }
            List<TOrderStyle> tOrderStyleList = tOrderStyleMapper.selectTOrderStyleList(t);
            for(TOrderStyle tOrderStyle : tOrderStyleList){
                tOrderStyle.setPayDate("");
                tOrderStyle.setPayMoney("");

                TOrderUserAddress f = new TOrderUserAddress();
                f.setOrderId(tOrder1.getId());
                f.setOrderStyleId(tOrderStyle.getId());
                List<TOrderUserAddress> tOrderUserAddresses = tOrderUserAddressMapper.selectTOrderUserAddressList(f);
                if(tOrderUserAddresses.size()>0){
                    tOrderStyle.setAddressNum(tOrderUserAddresses.get(0).getAddressNum());
                    tOrderStyle.setAddressImg(tOrderUserAddresses.get(0).getPaymentImgUrl());
                }

                TOrderUserPayment f1 = new TOrderUserPayment();
                f1.setOrderId(tOrder1.getId());
                f1.setOrderStyleId(tOrderStyle.getId());
                List<TOrderUserPayment> tOrderClientPayment = tOrderUserPaymentMapper.selectTOrderUserPaymentList(f1);
                if(tOrderClientPayment.size()>0){
                    tOrderStyle.setPayDate(DateUtils.dateTime(tOrderClientPayment.get(0).getCreateTime()));
                    tOrderStyle.setPayMoney(tOrderClientPayment.get(0).getPrice().toString());
                }
            }

            tOrder1.setMap(tOrderStyleList);
        }
        return list;
    }
    /**
     * 查询订单列表
     *
     * @param tOrder 订单
     * @return 订单
     */
    @Override
    public List<TOrder> selectTOrderList(TOrder tOrder)
    {

        List<TOrder> list = tOrderMapper.selectTOrderList(tOrder);
        for(TOrder tOrder1 : list){
            TOrderStyle t = new TOrderStyle();
            t.setOrderId(tOrder1.getId());
            List<TOrderStyle> tOrderStyleList = tOrderStyleMapper.selectTOrderStyleList(t);
            for(TOrderStyle tOrderStyle : tOrderStyleList){
                tOrderStyle.setPayDate("");
                tOrderStyle.setPayMoney("");

                TOrderClientPayment f = new TOrderClientPayment();
                f.setOrderId(tOrder1.getId());
                f.setStyleId(tOrderStyle.getId());
                List<TOrderClientPayment> tOrderClientPayment = tOrderClientPaymentMapper.selectTOrderClientPaymentList(f);
                if(tOrderClientPayment.size()>0){
                    tOrderStyle.setPayDate(DateUtils.dateTime(tOrderClientPayment.get(0).getCreateTime()));
                    tOrderStyle.setPayMoney(tOrderClientPayment.get(0).getPrice().toString());
                }

                TOrderUserAddress f1 = new TOrderUserAddress();
                f1.setOrderId(tOrder1.getId());
                f1.setOrderStyleId(tOrderStyle.getId());
                List<TOrderUserAddress> tOrderUserAddresses = tOrderUserAddressMapper.selectTOrderUserAddressList(f1);
                if(tOrderUserAddresses.size()>0){
                    tOrderStyle.setAddressNum(tOrderUserAddresses.get(0).getAddressNum());
                    tOrderStyle.setAddressImg(tOrderUserAddresses.get(0).getPaymentImgUrl());
                }
            }

            tOrder1.setMap(tOrderStyleList);
        }
        TOrder t = new TOrder();
        BigDecimal orderPrice = tOrderMapper.sumPrice(tOrder.getClientName());
        orderPrice = orderPrice!=null ? orderPrice : new BigDecimal(0);

        BigDecimal clientPrice = tOrderClientPaymentMapper.sumPrice(tOrder.getClientName());
        clientPrice = clientPrice!=null ? clientPrice : new BigDecimal(0);

        t.setOrderNum("总计定金"+orderPrice.add(clientPrice));

        t.setClientName("总计未付款"+tOrderMapper.sumBalance(tOrder.getClientName()));

        list.add(t);
        return list;
    }

    /**
     * 新增订单
     *
     * @param tOrder 订单
     * @return 结果
     */
    @Override
    public int insertTOrder(TOrder tOrder)
    {
        tOrder.setCreateTime(DateUtils.getNowDate());

        int num = tOrderMapper.countOrderNum(DateUtils.dateTimeNow());

        String d = "";
        if(num<10){
            d = "000"+num;
        }else if(num<100){
            d = "00"+num;
        }else if(num<1000){
            d = "0"+num;
        }
        tOrder.setOrderNum(DateUtils.dateTimeNow()+d);

        tOrderMapper.insertTOrder(tOrder);

        String styleList = "";
        int styleNum = 0;
        double price = 0.0;
        if(StringUtils.isNotEmpty(tOrder.getStyleList())){
            styleList = tOrder.getStyleList();
            JSONArray jsonArray = JSONArray.parseArray(styleList);
            for(Object obj : jsonArray){
                JSONObject jsonObject = (JSONObject)obj;
                TOrderStyle tOrderStyle = new TOrderStyle();
                tOrderStyle.setOrderId(tOrder.getId());
                tOrderStyle.setStyleName(jsonObject.getString("styleName"));
                tOrderStyle.setNum(jsonObject.getIntValue("num"));

                styleNum = styleNum + jsonObject.getIntValue("num");

                tOrderStyle.setLogo(jsonObject.getString("logo"));
                tOrderStyle.setPrice(jsonObject.getBigDecimal("price"));
                tOrderStyle.setCreateTime(DateUtils.getNowDate());
                tOrderStyle.setTotalPrice(jsonObject.getBigDecimal("totalPrice"));
                tOrderStyle.setUserId(jsonObject.getLong("userId"));
                tOrderStyle.setUserName(jsonObject.getString("userName"));

                price = price + (jsonObject.getIntValue("num") * jsonObject.getDoubleValue("price"));

                    tOrderStyleMapper.insertTOrderStyle(tOrderStyle);
            }
            tOrder.setNum(Long.parseLong(styleNum+""));
            tOrder.setTotalPrice(new BigDecimal(price));
        }
        tOrderMapper.updateTOrder(tOrder);

        return 1;
    }

    /**
     * 修改订单
     *
     * @param tOrder 订单
     * @return 结果
     */
    @Override
    public int updateTOrder(TOrder tOrder)
    {
        tOrder.setUpdateTime(DateUtils.getNowDate());

//        tOrderStyleMapper.deleteTOrderStyleById(tOrder.getId());

        String styleList = "";
        int styleNum = 0;
        double price = 0.0;
        if(StringUtils.isNotEmpty(tOrder.getStyleList())){
            styleList = tOrder.getStyleList();
            JSONArray jsonArray = JSONArray.parseArray(styleList);
            for(Object obj : jsonArray){
                JSONObject jsonObject = (JSONObject)obj;
                TOrderStyle tOrderStyle = new TOrderStyle();
                tOrderStyle.setOrderId(tOrder.getId());
                tOrderStyle.setStyleName(jsonObject.getString("styleName"));
                tOrderStyle.setNum(jsonObject.getIntValue("num"));
                tOrderStyle.setLogo(jsonObject.getString("logo"));
                tOrderStyle.setPrice(jsonObject.getBigDecimal("price"));
                tOrderStyle.setCreateTime(DateUtils.getNowDate());
                styleNum = styleNum + jsonObject.getIntValue("num");
                tOrderStyle.setTotalPrice(jsonObject.getBigDecimal("totalPrice"));

                tOrderStyle.setUserId(jsonObject.getLong("userId"));
                tOrderStyle.setUserName(jsonObject.getString("userName"));

                price = price + (jsonObject.getIntValue("num") * jsonObject.getDoubleValue("price"));
                if(jsonObject.getLong("id")==null){
                    tOrderStyleMapper.insertTOrderStyle(tOrderStyle);
                }else{
                    tOrderStyle.setId(jsonObject.getLong("id"));
                    tOrderStyleMapper.updateTOrderStyle(tOrderStyle);
                }
            }
            tOrder.setNum(Long.parseLong(styleNum+""));
            tOrder.setTotalPrice(new BigDecimal(price));
        }


        return tOrderMapper.updateTOrder(tOrder);
    }

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteTOrderByIds(Long[] ids)
    {
        return tOrderMapper.deleteTOrderByIds(ids);
    }

    /**
     * 删除订单信息
     *
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteTOrderById(Long id)
    {
        return tOrderMapper.deleteTOrderById(id);
    }
}
